﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;

namespace WinnerCMS.WebControls.BootStrap
{
    public class Button : System.Web.UI.WebControls.Button
    {
        /// <summary>
        /// 外观
        /// </summary>
        public BootStrapExterior Exterior { get; set; }

        private string _message;

        #region 属性

        [Browsable(true), Description("对话框的文字"), Category("对话框的文字")]
        public string Message
        {
            get { return this._message; }
            set { this._message = value; }
        }


        [Browsable(true), Description("动作"), Category("动作相关属性：None 为无，Confirm 选择对话框 ，ClickOnce发送表单的时候禁止点击多次"),
         DefaultValue(ActionX.None)]
        public ActionX ToDo
        {
            get
            {
                object obj = ViewState["ToDo"];
                return (obj == null) ? ActionX.None : (ActionX) obj;
            }
            set { ViewState["ToDo"] = value; }
        }

        [Browsable(true), Description("Validate"), Category("客户端验证的JS函数")]
        public string ValidateFunction
        {
            get
            {
                object obj = ViewState["ValidateFunction"];
                return (obj == null) ? "" : obj.ToString();
            }
            set { ViewState["ValidateFunction"] = value; }
        }


        [Browsable(true), Description("客户端事件"), Category("客户端"), DefaultValue(0)]
        public ClientEventOption ClientEvent
        {
            get
            {
                object obj = ViewState["ClientEvent"];
                return (obj == null) ? ClientEventOption.None : (ClientEventOption) obj;
            }
            set { ViewState["ClientEvent"] = value; }
        }

        [Browsable(true), Description("客户端脚本"), Category("客户端")]
        public string ClientEventScript
        {
            get
            {
                object obj = ViewState["ClientEventScript"];
                return (obj == null) ? "" : obj.ToString();
            }
            set { ViewState["ClientEventScript"] = value; }
        }

        [Browsable(true), Description("是否有验证控件"), Category("客户端"), DefaultValue(false)]
        public bool HasValidateControl
        {
            get
            {
                object obj = ViewState["HasValidateControl"];
                return (obj != null) && Convert.ToBoolean(obj);
            }
            set { ViewState["HasValidateControl"] = value; }
        }

        #endregion

        #region 重写OnPreRender

        /// <summary>
        /// 重写OnPreRender
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPreRender(EventArgs e)
        {
            StringBuilder script = new StringBuilder();

            #region 按钮的特别动作

            switch (this.ToDo)
            {
                case ActionX.ClickOnce:

                    if (ValidateFunction.Length == 0)
                    {
                        if (this.HasValidateControl)
                        {
                            this.Attributes.Add("onclick",
                                "if(WebForm_OnSubmit() == false) {document.getElementById(\"" + this.ClientID +
                                "\").disabled=true;" + Page.ClientScript.GetPostBackEventReference(this, "") + ";}");
                        }
                        else
                        {
                            this.Attributes.Add("onclick",
                                " document.getElementById(\"" + this.ClientID + "\").disabled=true;" +
                                Page.ClientScript.GetPostBackEventReference(this, "") + ";");
                        }
                    }
                    else
                    {
                        this.Attributes.Add("onclick",
                            ValidateFunction + "document.getElementById(\"" + this.ClientID + "\").disabled=true;" +
                            Page.ClientScript.GetPostBackEventReference(this, "") + ";");
                    }
                    break;

                case ActionX.Confirm:
                    this.Page.ClientScript.RegisterClientScriptBlock(this.Page.GetType(), "doalert",
                        @"function doconfirm(btnWaiter,msg) 
{
    if (confirm(msg)) 
    {
        btnWaiter.setAttribute(""value"",""请稍等..."");
        document.body.style.cursor=""wait"";
        return true;
     } 
    return false;
}", true);

                    if (this._message.Length > 0)
                    {
                        this.Attributes["onclick"] = "javascript:return doconfirm(this,'" + this._message + "');";
                    }
                    break;

                default:
                    break;
            }

            #endregion

            base.OnPreRender(e);

            if (ClientEvent != ClientEventOption.None)
            {
                this.Attributes.Add(ClientEvent.ToString(), ClientEventScript);
            }
            if (Exterior == BootStrapExterior.None)
            {
                this.CssClass = "btn btn-default" + " " + this.CssClass;
            }
            else
            {
                this.CssClass = "btn btn-" + Exterior.ToString() + " " + this.CssClass;
            }

        }

        #endregion



    }

    /// <summary>
    /// 动作
    /// </summary>
    public enum ActionX : byte
    {
        None,
        Confirm,
        ClickOnce
    }

    /// <summary>
    /// BootStrap外观
    /// </summary>
    public enum BootStrapExterior : byte
    {
        /// <summary>
        /// 默认
        /// </summary>
        None = 0,

        /// <summary>
        /// 提供额外的视觉感, 可在一系列的按钮中指出主要操作
        /// </summary>
        primary,

        /// <summary>
        /// 默认样式的替代样式
        /// </summary>
        info,

        /// <summary>
        /// 表示成功或积极的动作
        /// </summary>
        success,

        /// <summary>
        /// 提醒应该谨慎采取这个动作
        /// </summary>
        warning,

        /// <summary>
        /// 表示这个动作危险或存在危险
        /// </summary>
        danger,

        /// <summary>
        /// 备用的暗灰色按钮，不依赖于语义和用途
        /// </summary>
        inverse,

        /// <summary>
        /// 简化一个按钮, 使它看起来像一个链接，同时保持按钮的行为
        /// </summary>
        link
    }

    public enum ClientEventOption : byte
    {
        None = 0,
        onblur,
        onchange,
        onclick,
        ondblclick,
        onfocus,
        onkeydown,
        onkeypress,
        onkeyup,
        onmousedown,
        onmousemove,
        onmouseout,
        onmouseover,
        onmouseup,
        onselect

    }
}